package tq.com.tqteacher.model;

import com.github.barteksc.pdfviewer.util.ArrayUtils;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

import tq.com.systools.system.StringHelper;
import tq.com.tqcom.net.INetCallback;
import tq.com.tqteacher.net.TQNetInterface;
import tq.com.tqteacher.propertyEnum.TQModify;

public class TQAtachModel {
    private String desc;
    private ArrayList<String> files = new ArrayList<>();
    private TQUserInfo mUserInfo;
    private String date;
    private int readCount;
    private int commentCount;
    private int id;

    private String type;
    private String cover;

    private TQCommentOverInfoModel.TQCommentInfoModel mCommentInfoModel;

    public TQAtachModel(String pDesc, ArrayList<String> pFiles){
        desc = pDesc;

        if(pFiles != null){
            files.addAll(pFiles);
        }
    }
    public TQAtachModel(){}
    public TQAtachModel(String file){
        files.add(file);
    }
    public TQAtachModel(String file, String pCover){
        this.cover = pCover;
        files.add(file);
    }

    public HashMap<String, String> getHashInfo(){
        HashMap<String, String> lHashMap = new HashMap<>();
        if(desc != null && desc.length() > 0){
            lHashMap.put("desc", desc);
        }

        if(cover != null && cover.length() > 0){
            lHashMap.put("cover", cover);
        }
        if(type != null && type.length() > 0){
            lHashMap.put("type", cover);
        }

        if(files.size() > 0){
            StringBuffer lBuffer = new StringBuffer();
            for (String s:
                    files) {
                if(lBuffer.length() > 0){
                    lBuffer.append(",");
                }
                lBuffer.append(s);
            }
            lHashMap.put("filelist", lBuffer.toString());
        }

        return lHashMap;
    }
    public String getDesc() {
        return desc;
    }

    public void setDesc(String pDesc) {
        desc = pDesc;
    }

    public ArrayList<String> getFiles() {
        return files;
    }

    public void add(String file){
        files.add(file);
    }
    public void add(ArrayList<String> pFiles){
        files.addAll(pFiles);
    }

    public void fromJson(JSONObject pObject){
        if(pObject == null) return;

        desc = pObject.optString("desc");
        id = pObject.optInt("id");
        String files = pObject.optString("filelist");
        ArrayList<String> tmps = StringHelper.splite(files, ",");
        if(tmps != null && tmps.size() > 0){
            this.files.clear();
            this.files.addAll(tmps);
        }

        date = pObject.optString("update_time");
        readCount = pObject.optInt("read_cnt");
        commentCount = pObject.optInt("comment_cnt");
        cover = pObject.optString("cover");
        type = pObject.optString("type");

        JSONObject c = pObject.optJSONObject("creator");
        if(c == null) return;

        if(mUserInfo == null){
            mUserInfo = new TQUserInfo();
        }
        mUserInfo.refresh(c);
    }

    public void refreshComment(JSONObject pJSONObject){
        if(pJSONObject == null) return;
        if(getCommentCount() == 0) return;

        if(mCommentInfoModel == null){
            mCommentInfoModel = new TQCommentOverInfoModel.TQCommentInfoModel();
        }

        mCommentInfoModel.refresh(pJSONObject);
        if(mCommentInfoModel.getId() == 0){
            mCommentInfoModel = null;
        }
    }

    public TQCommentOverInfoModel.TQCommentInfoModel getComment() {
        return mCommentInfoModel;
    }
    public void setCommentInfoModel(TQCommentOverInfoModel.TQCommentInfoModel pInfoModel){
        mCommentInfoModel = pInfoModel;
    }

    public int getId(){
        return id;
    }
    public TQUserInfo getUserInfo() {
        return mUserInfo;
    }

    public void setUserInfo(TQUserInfo pUserInfo) {
        mUserInfo = pUserInfo;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String pDate) {
        date = pDate;
    }

    public int getReadCount() {
        return readCount;
    }

    public void setReadCount(int pReadCount) {
        readCount = pReadCount;
    }

    public int getCommentCount(){
        return commentCount;
    }
    public void setCommentCount(int pCount){
        commentCount = pCount;
    }
    public String getFirstFile(){
        if(files == null || files.size() == 0) return null;

        return files.get(0);
    }
    public void replaceFile(String file){
        files.clear();
        files.add(file);
    }

    public boolean hasCover(){
        return !StringHelper.isEmpty(cover) ;
    }
    public boolean hasFile(){
        String file = getFirstFile();
        return !StringHelper.isEmpty(file);
    }

    public String getType() {
        return type;
    }

    public void setType(String pType) {
        type = pType;
    }

    public String getCover() {
        return cover;
    }
    public void setCover(String pCover) {
        cover = pCover;
    }
    public void doModifyAttach(INetCallback pNetCallback){
        HashMap<TQModify.AttrachInfo, String> lInfoStringHashMap = getModifyInfo();

        TQNetInterface.modifyAttachInfo(id, lInfoStringHashMap, pNetCallback);
    }
    public HashMap<TQModify.AttrachInfo, String> getModifyInfo(){
        HashMap<TQModify.AttrachInfo, String> lInfoStringHashMap = new HashMap<>();
        if(!StringHelper.isEmpty(getCover())){
            lInfoStringHashMap.put(TQModify.AttrachInfo.cover, getCover());
        }

        if(!StringHelper.isEmpty(getDesc())){
            lInfoStringHashMap.put(TQModify.AttrachInfo.desc, getDesc());
        }
        if(getFiles().size() > 0){
            String files = StringHelper.join(getFiles(), ",");
            lInfoStringHashMap.put(TQModify.AttrachInfo.filelist, files);
        }

        return lInfoStringHashMap;
    }
}
